package com.zhongkai.tools;

import java.io.*;
import java.net.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import javax.servlet.jsp.JspWriter;


public class PageTool
{
  static String contentType = "text/html; charset=utf-8";
  static Logger log = Logger.getLogger("PageTool");

//创建printWriter
  private static PrintWriter createPrintWriter(HttpServletResponse response) throws
      IOException
  {
    PrintWriter printWriter = response.getWriter();
    return printWriter;
  }

//关闭printWriter
  private static void closePrintWriter(PrintWriter printWriter)
  {
    if (printWriter != null)
    {
      printWriter.close();
    }
  }

  /**
   *
   * 在本窗口打出，并打印输出信息
   * @param response HttpServletResponse
   * @param message String
   * @param url String
   */
  public static void selfLocationAssign(HttpServletResponse response,String message,String url)
  {
    response.setContentType(contentType);
    PrintWriter printWriter = null;
    try{

      printWriter = createPrintWriter(response);
      printWriter.print(
          "<script language=\"javascript\">alert(\""+message+
          "\");self.location.assign(\""+url+"\");</script>");

    }catch(IOException ioe){
      log.info("执行PageTool的self.location.assign()"+ioe.getMessage());
    }



  }


  //执行显示location.assign()操作
  public static void locationAssign(HttpServletResponse response, String message,
                                    String url)
  {
    response.setContentType(contentType);
    PrintWriter printWriter = null;
    try
    {
      printWriter = createPrintWriter(response);
      printWriter.print(
          "<script language=\"javascript\">alert(\"" + message +
          "\");location.assign(\"" + url + "\");</script>");
    }
    catch (IOException ioe)
    {
      log.info("执行PageTool的locationAssign():" + ioe.getMessage());
    }
    finally
    {
      closePrintWriter(printWriter);
    }
  }
  //执行显示location.assign()操作
public static void parentLocationAssign(HttpServletResponse response, String message,
                                  String url)
{
  response.setContentType(contentType);
  PrintWriter printWriter = null;
  try
  {
    printWriter = createPrintWriter(response);
    printWriter.print(
        "<script language=\"javascript\">alert(\"" + message +
        "\");window.parent.location.assign(\"" + url + "\");</script>");
  }
  catch (IOException ioe)
  {
    log.info("执行PageTool的locationAssign():" + ioe.getMessage());
  }
  finally
  {
    closePrintWriter(printWriter);
  }
}

  //执行显示location.assign()操作
  public static void locationAssign(HttpServletResponse response,
                                    String url)
  {
    response.setContentType(contentType);
    PrintWriter printWriter = null;
    try
    {
      printWriter = createPrintWriter(response);
      printWriter.print(
          "<script language=\"javascript\">location.assign(\"" + url + "\");</script>");
    }
    catch (IOException ioe)
    {
      log.info("执行PageTool的locationAssign():" + ioe.getMessage());
    }
    finally
    {
      closePrintWriter(printWriter);
    }
  }
//执行显示history.back()操作
  public static void historyBack(HttpServletResponse response, String message)
  {
    response.setContentType(contentType);
    PrintWriter printWriter = null;
    try
    {
      printWriter = createPrintWriter(response);
      printWriter.print(
          "<script language=\"javascript\">alert(\"" + message +
          "\");history.back();</script>");
    }
    catch (IOException ioe)
    {
      log.info("执行PageTool的historyBack():" + ioe.getMessage());
    }
    finally
    {
      closePrintWriter(printWriter);
    }
  }

//使用printWriter在页面中进行输出操作
  public static void pagePrint(HttpServletResponse response, String message)
  {
    response.setContentType(contentType);
    PrintWriter printWriter = null;
    try
    {
      printWriter = createPrintWriter(response);
      printWriter.print(message);
    }
    catch (IOException ioe)
    {
      log.info("执行PageTool的pagePrint():" + ioe.getMessage());
    }
    finally
    {
      closePrintWriter(printWriter);
    }
  }

//弹出提示语，并关闭窗口
  public static void closeWindow(HttpServletResponse response, String message)
  {
    response.setContentType(contentType);
    PrintWriter printWriter = null;
    try
    {
      printWriter = createPrintWriter(response);
      printWriter.print("<script language=\"javascript\">alert(\"" + message +
                        "\");window.close();</script>");
    }
    catch (IOException ioe)
    {
      log.info("执行PageTool的closeWindow():" + ioe.getMessage());
    }
    finally
    {
      closePrintWriter(printWriter);
    }
  }
  //弹出提示语，并关闭窗口
    public static void alert(HttpServletResponse response, String message)
    {
      response.setContentType(contentType);
      PrintWriter printWriter = null;
      try
      {
        printWriter = createPrintWriter(response);
        printWriter.print("<script language=\"javascript\">alert(\"" + message +
                          "\");</script>");
      }
      catch (IOException ioe)
      {
        log.info("执行PageTool的closeWindow():" + ioe.getMessage());
      }
      finally
      {
        closePrintWriter(printWriter);
      }
  }
  //弹出下载页面
    public static void downloadWindow(HttpServletResponse response,String downLoadContent, String displayName)
    {
      response.setContentType(contentType);
      try
      {
        displayName = URLEncoder.encode(displayName, "UTF-8");
      }
      catch (UnsupportedEncodingException e)
      {
        log.info("进行字符集转换时抛出异常:" + e.getMessage());
      }

      response.setHeader("Content-disposition",
                         "attachment;filename=" + displayName);
      DataOutputStream dataOutputStream = null;
      try
      {
      byte[] downLoadContentBytes=downLoadContent.getBytes("UTF-8");
      String contentLength = String.valueOf(downLoadContentBytes.length);
      response.setHeader("Content-Length", contentLength);
        dataOutputStream =new DataOutputStream(response.getOutputStream());
          dataOutputStream.write(downLoadContentBytes);
      }
      catch (Exception e)
      {
        //log.error("下载文件时抛出异常");
      }
      finally
      {
        if (dataOutputStream != null)
        {
          try
          {
            dataOutputStream.close();
          }
          catch (IOException ioe)
          {
            log.info("执行PageTool的downloadWindow(),关闭转出流OutputStream时抛出异常:" +
                     ioe.getMessage());
          }
        }
      }
  }
//弹出下载页面
  public static void downloadWindow(HttpServletResponse response,
                                    String fileRealPath, String displayName,
                                    long fileSize)
  {
    response.setContentType(contentType);
    try
    {
      displayName = URLEncoder.encode(displayName, "UTF-8");
    }
    catch (UnsupportedEncodingException e)
    {
      log.info("进行字符集转换时抛出异常:" + e.getMessage());
    }

    response.setHeader("Content-disposition",
                       "attachment;filename=" + displayName);

    String contentLength = String.valueOf(fileSize);
    response.setHeader("Content-Length", contentLength);

    BufferedInputStream bufferedInputStream = null;
    OutputStream outputStream = null;
    try
    {
      outputStream = response.getOutputStream();
      bufferedInputStream = new BufferedInputStream(new FileInputStream(
          fileRealPath));
      int bytesRead = 0;
      final int length = 1024;
      byte[] buffer = new byte[length];
      while ((bytesRead = bufferedInputStream.read(buffer, 0, length)) != -1)
      {
        outputStream.write(buffer, 0, bytesRead);
      }
    }
    catch (Exception e)
    {
      //log.error("下载文件时抛出异常");
    }
    finally
    {
      if (bufferedInputStream != null)
      {
        try
        {
          bufferedInputStream.close();
        }
        catch (IOException ioe)
        {
          log.info("执行PageTool的downloadWindow(),关闭转入流FileinputStream时抛出异常:" +
                   ioe.getMessage());
        }
      }
      if (outputStream != null)
      {
        try
        {
          outputStream.close();
        }
        catch (IOException ioe)
        {
          log.info("执行PageTool的downloadWindow(),关闭转出流OutputStream时抛出异常:" +
                   ioe.getMessage());
        }
      }
    }
  }
//在标签中可以调用此函数进行输出操作JspWriter=pageContext.getOut();
  public static void jspPrint(JspWriter out, String content)
  {
    try
    {
      out.print(content);
    }
    catch (IOException ioe)
    {
      log.error("利用标签进行输出jsp代码时抛出异常:" + ioe.getMessage());
    }
  }
}
